import React from 'react';
import CheckWanning from './../common/errorMessage.js';

export default class SafeInfo extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      'cbtn':false,
      userinfo:this.props.user.userinfo,
      emailError: ''
    }
    this.input_number = this.input_number.bind(this);
  }

  input_number(event) {
    const email = event.target.value;
    this.emailCheck(email);
  }

  emailCheck(email) {
    const afterprefix = ['139.com','163.com','qq.com'];
    let aind = email.indexOf('@');
    let pind = email.indexOf('.');
    let last = email.substring(aind+1);
    if(aind<0 || pind<aind || !this.checkLast(last,afterprefix)) {
      this.setState({
        emailError: '错误的邮箱格式',
      });
      return false;
    } else {
      this.setState({
        emailError: '',
      });
      return true;
    }
  }

  checkLast(last,array) {
    for(var i=0;i<array.length;i++) {
      if(last==array[i]) {
        return true;
      }
    }
    return false;
  }

  render() {
    return (
      <div className="save_info_box">

        <div className="person_title">
            安全认证
        </div>
        <div className="safe-box">
          <div className="input_control">
            <div>
              <input type="text" placeholder="请输入你的邮箱" defaultValue={this.state.userinfo?(this.state.userinfo.phoneNumber?this.state.userinfo.phoneNumber:''):''} onBlur={this.input_number.bind(this)}/>
            </div>
            <button className="btn btn-success" style={{marginLeft:'18px',marginTop:'8px',verticalAlign:'bottom'}} disabled={true}>发送验证码</button>
            <div className="clear"></div>
            <CheckWanning message={this.state.emailError} />  
          </div>
          <div className="input_control">
            <div>
              <input type="text" placeholder="输入验证码"/>
            </div>
            
            <div className="clear"></div>
          </div>
        </div>
      </div>
      
      
    );
  }
}